package com.v1.video.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.GuideRecommandActivity;
import com.v1.video.activity.MainPageNewActivity;
import com.v1.video.util.Helper;

public class GuideFragment extends Fragment implements OnClickListener {

    private int index;
    private View goV;
    private ImageView guideIv;
    private Activity activity;
    
    public static GuideFragment newInstance(int index) {
        GuideFragment myFragment = new GuideFragment();

        Bundle args = new Bundle();
        args.putInt("index", index);
        myFragment.setArguments(args);

        return myFragment;
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }
    
    @Override
    public void onDetach() {
        super.onDetach();
        this.activity = null;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        index = getArguments().getInt("index");
        if(savedInstanceState != null )
        {
        	index = savedInstanceState.getInt("index", 4);
        }
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("index", index);
    }
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_guide, container, false);

		initView(view);
		initDate();
		setListener(view);

		return view;
	}

	private void initView(View view) {
	    goV = view.findViewById(R.id.v_go);
	    guideIv = (ImageView) view.findViewById(R.id.iv_guide);
	}

	private void initDate() {
	    switch (index) {
            case 0:
                guideIv.setImageResource(R.drawable.guide1);
                goV.setVisibility(View.GONE);
                break;
            case 1:
                guideIv.setImageResource(R.drawable.guide2);
                goV.setVisibility(View.VISIBLE);
                break;
            case 2:
//                guideIv.setImageResource(R.drawable.guide3);
//                goV.setVisibility(View.VISIBLE);
                break;
//            case 3:
//                guideIv.setImageResource(R.drawable.guide4);
//                goV.setVisibility(View.VISIBLE);
//                break;
//            case 4:
//                guideIv.setImageResource(R.drawable.guide5);
//                goV.setVisibility(View.VISIBLE);
//                break;
        }
	}

	private void setListener(View view) {
	    goV.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
	    switch (v.getId()) {
            case R.id.v_go:
                goToHomePage(v.getContext());
                break;

        }
	}

	private void goToHomePage(Context context){
//	    SharedPreferences sp = context.getSharedPreferences(Constant.APP_INFO, context.MODE_PRIVATE);
//	    Editor editor = sp.edit();
//	    editor.putBoolean("first_start", false);
//	    editor.commit();
	    //startActivity(new Intent(context,HomepageActivity.class));
	    Helper helper = new Helper();
		if(helper.checkConnection(getActivity(), 0)){
			startActivity(new Intent(context,GuideRecommandActivity.class));
		}else{
		    startActivity(new Intent(context,MainPageNewActivity.class));
		}
		    if(activity != null){
		        activity.finish();
		    }else{
		        activity = getActivity();
		        if(activity != null){
		            activity.finish();
		        }
		    }
	    }
	
}
